function onModuleLoad() {
	var $cityModule = $("#cityModule");
	var $cityModuleSearch = $("#cityModule_search_button",$cityModule);
	var $districtToSearchList =  $("#districtToSearchList");
	$("#accordion").accordion('activate', 1);
	
	var $stateToSearchList =  $("#stateToSearchList").change(function() {
		  // user changed the state. call server to get new district list 
		// remove all the items from the district list
		$districtToSearchList.empty();
		var stateId = $(this).attr('value');;
			
		$.getJSON('http://localhost:8080/smartbillpay/public/locations/districts/state/' +stateId , function(data) {
			 
			  $.each(data, function(key, val) {
				  $districtToSearchList.append(new Option(val.name, val.id ));
			  });
			});				
		});
	
	$cityModuleSearch.click(function(){
		// call server to get the list of districts in a given state
		 var districtId = "";
	 	 $("select option:selected").each(function () {
	 		 districtId = $(this).attr('value');
	      });
	 	var url = "../city/district/" + districtId;
	 	window.location = url;
	});
};
	
function onCityListLoad() {
	var $cityModule = $("#cityModule");
	var $city_add_form = $("#cityModule_add_form");
	var $cityTable = $("#cityModule_table",$cityModule);
	var deleteCityName;
	var deleteCityUrl;
	var deleteCityId;
	var cityRowToDelete;
	
	// following script stops the submit event of form when user clicks the Enter button
	$('.noEnterSubmit').keypress(function(e){
	  	if ( e.which == 13 ) return false;
	  	
	});
	var $city_add_dialog = $( "#cityModule_add_dialog" ).dialog({
		autoOpen: false,
		modal: true,
		autoOpen: false,
		width: 440,
		buttons: {
			Add: function() {
				if($city_add_form.valid())
				{
				    var url = $city_add_form.attr( 'action' );
				    var jqxhr = $.post(url,$city_add_form.serialize(), function(data) {
				   			
							  if(data.status == "Success"){
								  $cityTable.append("<tr> <td>" +data.resultMap.newCityName + "</td> <td><a class=\"cityModuleDeleteCity\" href=\"../delete/$" +data.resultMap.newCityId +"\">Delete</a></td> </tr>");
									 
								  $city_add_dialog.dialog( "close" );
								  
								  $("#cityModule p").remove();
								  
								      }
					      else {
					    	  $serverErrormessage = $( "#cityModule_add_dialog_status" , $city_add_dialog);
					    	  $serverErrormessage.text("Error adding new city - " + data.errorMap.errorMessage);
					      }					   	  
					    }, "json");					
				}
			},
			Cancel: function() {
				$( this ).dialog( "close" );
			}
		},
		open: function() {
			$tab_title_input.focus();
		},
		close: function() {
			$city_add_form[ 0 ].reset();
			$serverErrormessage.text("");
		}
	});
	
	// addTab button: just opens the dialog
	$( "#cityModule_add_button" ).click(function() {
		$city_add_dialog.dialog( "open" );
		});
	
	var $city_delete_dialog = $("#cityModule_delete_dialog", $cityModule ).dialog({
		autoOpen: false,
		modal: true,
		autoOpen: false,
		width: 440,
		buttons: {
			Yes: function() {
				var jqxhrDelete = $.post(deleteCityUrl,{cityId:deleteCityId}, function(data) {
					if(data.status == "Success"){
						  cityRowToDelete.remove();
						  $city_delete_dialog.dialog( "close" );				    	
				      }
				      else {
				    	  var $serverErrormessage = $( "#cityModule_delete_dialog_status" , $city_delete_dialog);
				    	  $serverErrormessage.text("Error deleting the city - " + data.errorMap.errorMessage);
				      }				    	  
				    }, "json");		
						
			},
			No: function() {
				$( this ).dialog( "close" );
			}
		},
		open: function() {
			//$tab_title_input.focus();
		},
		close: function() {
			$serverErrormessage.text("");
		}
	});
	
	//var $stateTable = $("#stateModule_table",$stateModule);
	var $cityDeleteLink = $("a", $cityTable);
	$cityDeleteLink.live("click",function(event) {
        event.preventDefault();
        cityRowToDelete = $(this).parent().parent();
        deleteCityName = $(this).parent().siblings().html();
        deleteCityUrl = $(this).attr('href');
        var hrefArray= deleteCityUrl.split('$'); 
        deleteCityUrl = hrefArray[0];
        deleteCityId = hrefArray[1];
        $("#cityModule_delete_message",$city_delete_dialog).text(deleteCityName);
        $city_delete_dialog.dialog( "open" );
       
    });
};